package com.hurl.waguji.web.cx;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.hurl.library.jdbc.DBManager;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.SqlReader;
import com.hurl.waguji.base.dao.HibernateDao;
import com.hurl.waguji.base.dao.HibernateService;
import com.hurl.waguji.persistence.DzcxBO;
import com.hurl.waguji.persistence.DzcxColumnBO;
import com.opensymphony.xwork2.ActionSupport;

public class DzcxAction extends ActionSupport{
	private Map<String,Object> result = new HashMap<String,Object>();
	private Integer dzcxId;
	private int start = 0;
	private int limit = 20;
	
	
	public void setStart(int start) {
		this.start = start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public void setDzcxId(Integer dzcxId) {
		this.dzcxId = dzcxId;
	}

	public Map<String, Object> getResult() {
		return result;
	}

	public String list()throws Exception{
		HibernateDao dao = HibernateService.getDao();
		DBManager jdbc = dao.getJDBC();
		
		List<Map<String,Object>> list = (List<Map<String,Object>>)jdbc.query(SqlReader.getSql("SQL12110801"));
		
		result.put("success", 1);
		result.put("data", list);
		
		return SUCCESS;
	}
	
	public String item()throws Exception{
		result.put("success", 1);
		HibernateDao dao = HibernateService.getDao();
		if(dzcxId!=null){
			DzcxBO bo = dao.get(DzcxBO.class, this.dzcxId);
			if(bo!=null){
				String bt = bo.getBt();
				if(StringUtils.isNotBlank(bt)){
					bo.setBt(bt.replace("\n", "<br>"));
				}
				result.put("dzcx", bo);
				List<DzcxColumnBO> columnlist = (List<DzcxColumnBO>)dao.createCriteria(DzcxColumnBO.class).add(Restrictions.eq("dzcxId", bo.getId())).addOrder(Order.asc("idx")).list();
				if(columnlist!=null && !columnlist.isEmpty()){
					result.put("column", columnlist);
				}
			}
		}
		return SUCCESS;
	}
	
	public String data()throws Exception{
		if(this.dzcxId!=null){
			HibernateDao dao = HibernateService.getDao();
			DzcxBO bo = dao.get(DzcxBO.class, this.dzcxId);
			if(bo!=null && StringUtils.isNotBlank(bo.getSqlcode())){
				/* query the total count */
				int totalProperty = ((BigInteger)dao.queryBySQL("select count(*) from ("+bo.getSqlcode()+") t").list().get(0)).intValue();
				
				/* query data */
				List<Map<String,Object>> root = new ArrayList<Map<String,Object>>();
				String sql = "select * from ("+bo.getSqlcode()+") t limit "+this.start+","+this.limit;
				root = dao.getJDBC().query(sql);
				result.put("totalProperty", totalProperty);
				result.put("root", root);
			}
		}
		return SUCCESS;
	}
	private Map<Integer,String> findColumnNames(String sql){
		if(StringUtils.isBlank(sql))return null;
		sql = sql.toLowerCase();
		String select = StringUtils.substringBetween(sql, "select", "from");
		if(StringUtils.isBlank(select))return null;
		
		String[] columns = select.split(",");
		if(columns==null || columns.length==0)return null;
		
		Map<Integer,String> result = new HashMap<Integer,String>();
		for(int i=0;i<columns.length;i++){
			String column = columns[i];
			String[] s = column.split(" |as");
			if(s.length>=2){
				result.put(i,s[s.length-1].trim());
			}else{
				result.put(i,s[0].trim());
			}
		}
		return result;
	}
}
